package gzip;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class ZipCompressUtil {
	
	public static void main(String[] args) {  
        try {  
//            zip("F:\\git\\test", "F:\\git\\test.zip");  
            unZip("F:\\git\\test.zip", "F:\\git\\mytest");
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
  
    }  
	
	private static byte[] _byte = new byte[1024] ;
	
	/** 压缩文件
	 * @param inputFile  源文件名
	 * @param zipFileName 目标压缩文件名
	 * @throws Exception
	 */
	public static void zip(String inputFileName, String zipFileName) throws Exception {  
        System.out.println("压缩中...");  
        long startTime=System.currentTimeMillis();
        
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));  
        BufferedOutputStream bo = new BufferedOutputStream(out);  
        File inputFile = new File(inputFileName);
        doZip(out, bo, inputFile, inputFile.getName());  
        bo.close();  
        out.close(); // 输出流关闭  
        
        System.out.println("压缩完成");  
        long endTime=System.currentTimeMillis();
		System.out.println("耗费时间： "+(endTime-startTime)+" ms");
    }  
	
	/**
     * @param out 用于创建zip压缩进入点base  
     * @param bo 写入普通文件信息
     * @param f 要压缩的文件对象
     * @param base 当前文件目录
     * @throws Exception
     */
    private static void doZip(ZipOutputStream out, BufferedOutputStream bo, File f, String base) throws Exception { // 方法重载  
        if (f.isDirectory()) {  
            File[] fl = f.listFiles();  
            out.putNextEntry(new ZipEntry(base + "/")); // 创建zip压缩进入点base  
            for (int i = 0; i < fl.length; i++) {  
            	doZip(out, bo, fl[i], base + "/" + fl[i].getName()); // 递归遍历子文件夹  
            }  
        } else {  
            out.putNextEntry(new ZipEntry(base)); // 创建zip压缩进入点base  
            BufferedInputStream bi = new BufferedInputStream(new FileInputStream(f));  
            int len;  
            while ((len = bi.read(_byte)) != -1) {  
                bo.write(_byte, 0, len); // 将字节流写入当前zip目录  
            }  
            bi.close();  
        }  
    }  
    
    /** 解压文件
     * @param srcFile
     * @param destFolder
     */
    public static void unZip(String srcFile, String destFolder) {
    	System.out.println("解压中...");  
		long startTime=System.currentTimeMillis();
		try {
			ZipInputStream zIn=new ZipInputStream(new FileInputStream(srcFile));//输入源zip路径
			BufferedInputStream bIn=new BufferedInputStream(zIn);
			File file = null;
			ZipEntry entry;
			try {
				while((entry = zIn.getNextEntry())!=null ){
					//遍历zip入口
					file=new File(destFolder,entry.getName());
					if(entry.isDirectory()) {
						mkdirs(file);
					} else {
						mkdirs(file.getParentFile());
						BufferedOutputStream bOut=new BufferedOutputStream(new FileOutputStream(file));
						int len;
						while((len=bIn.read(_byte))!=-1){
							bOut.write(_byte, 0, len);
						}
						bOut.close();
					}
				}
				bIn.close();
				zIn.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println("解压完成");  
		long endTime=System.currentTimeMillis();
		System.out.println("耗费时间： "+(endTime-startTime)+" ms");
	}
    
    private static void mkdirs(File file) {
    	if(file != null) {
    		File parent = file.getParentFile();
        	if(parent != null && !parent.exists()) {
        		mkdirs(parent);
        	}
        	
        	if(!file.exists()) {
        		file.mkdirs();
        	}
    	}
    }
}
